﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Client;
using Google.GData.Extensions;
using System.Xml;
using DataType;
using DataType;
using RemoteConnector;

namespace GoogleConnector
{

    public class SitesConnector : ICMSRemoteConnetor
    {

        private SitesService m_SiteService;

        public SitesConnector()
        {
            m_SiteService = null; 
        }

        public void Login(object sender, SiteInfo cred)
        {
            //throw new NotImplementedException();
            m_SiteService = new SitesService(cred.m_AppName,
                                             cred.m_Domain,
                                             cred.m_SiteName,
                                             cred.m_Cred);
        }

        public void LogOut()
        {
            //NOP         
        }

        public void GetContentFeed(object sender, AtomFeedWrapper feed)
        {
            AtomFeed aFeed = m_SiteService.getContentFeed();
            feed.m_AtomFeed = aFeed;
        }

        private String getCategoryLabel(AtomCategoryCollection categories)
        {
            foreach (AtomCategory cat in categories)
            {
                if (cat.Scheme == SitesService.KIND_SCHEME)
                {
                    return cat.Label;
                }
            }
            return null;
        }

        public void GetFullTextContent(object sender, StringWrapper content)
        {
            string result = "";
            AtomFeed feed = m_SiteService.getContentFeed(); ;

            if (feed.Entries.Count == 0) return;

            foreach (AtomEntry entry in feed.Entries)
            {
                String pageType = getCategoryLabel(entry.Categories);
                result += String.Format("Page: {0} ({1})", entry.Title.Text, pageType);
                result += String.Format("  link: {0}", entry.AlternateUri);
                AtomPersonCollection authors = entry.Authors;
                foreach (AtomPerson author in authors)
                {
                    result += String.Format("\tauthor: {0} - {1}", author.Name, author.Email);
                }
                String pageContent = entry.Content.Content;
                result += String.Format("  html: {0}...", pageContent);
            }

            content.m_string = result;
        }



        #region ICMSRemoteConnetor Members


        public void UpdatePageContent(object sender, PageInfo page)
        {
            this.m_SiteService.UpdateWebPage(page.m_AtomEntry);
        }

        #endregion
    }

    class SiteEntry : AbstractEntry
    {
        public SiteEntry() : base() { }
    }

    class SitesService : MediaService
    {
        public const string GSitesService = "jotspot";
        public const string SITES_NAMESPACE = "http://schemas.google.com/sites/2008";
        public const string KIND_SCHEME = "http://schemas.google.com/g/2005#kind";
        public const string ATTACHMENT_TERM = SITES_NAMESPACE + "#attachment";
        public const string WEBPAGE_TERM = SITES_NAMESPACE + "#webpage";
        public const string FILECABINET_TERM = SITES_NAMESPACE + "#filecabinet";
        public const string PARENT_REL = SITES_NAMESPACE + "#parent";

        public SitesService(string applicationName, string domain, string sitename, Credentials Cred)
            : base(GSitesService, applicationName)
        {
            m_app_name = applicationName;
            m_domain = domain;
            m_sitename = sitename;
            this.setUserCredentials(Cred.m_Cred.Username, Cred.m_password);
        }

        private string m_app_name = "Uprise Apps Google Sites Connector";
        private string m_domain;
        private string m_sitename;

        private String makeFeedUri(String type)
        {
            return String.Format("http://sites.google.com/feeds/{0}/{1}/{2}/", type, m_domain, m_sitename);
        }

        public AtomFeed getContentFeed()
        {
            return getContentFeed(makeFeedUri("content"));
        }

        public AtomFeed getContentFeed(String feedUri)
        {
            FeedQuery query = new FeedQuery(feedUri);
            AtomFeed feed = this.Query(query);
            return feed;
        }



        public AtomEntry UpdateWebPage(AtomEntry entry)
        {
            //entry.Title.Text = title;
            //entry.Content.Content = html;
            
            try
            {
                //entry.Content.Content = "<div xmlns=\"http://www.w3.org/1999/xhtml\">Updated Content</div>";
                entry.Update();
            }
            catch (GDataRequestException e)
            {
                
            }

            return entry;
        }

    }
}
